home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2121 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: news.mindlink.net!news
  2. From: genew@mindlink.bc.ca (Gene Wirchenko)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Examples of using "volatile"?
  5. Date: Fri, 19 Jan 1996 06:53:15 GMT
  6. Organization: MIND LINK! - British Columbia, Canada
  7. Message-ID: <4dnf7f$1sa@fountain.mindlink.net>
  8. References: <4djoj2$mr1@post.gsfc.nasa.gov> <4dm91p$bsi@info.uah.edu>
  9. NNTP-Posting-Host: line121.nwm.mindlink.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. gbacon@oreo (Greg Bacon) wrote:
  13.  
  14. >Stephen Maher (Stephen.Maher@gsfc.nasa.gov) wrote:
  15. >: Hi,
  16.  
  17. >: I'd like a concrete example(s) illustrating a reason to
  18. >: use the "volatile" type qualifier.
  19.  
  20. >: I understand the *theory* behind volatile (e.g., to
  21. >: avoid unwanted side-effects?).  I also realize it's
  22. >: probably implementation dependent. But some examples
  23. >: from any implementation should help me understand
  24. >: its use a little better.
  25.  
  26. >: Thanks,
  27.  
  28. >: Steve
  29.  
  30. >OK... say for example you have this:
  31.  
  32. >void foo(void)
  33. >{
  34. >   int i = 7;
  35.  
  36. >   if (i == 7)
  37. >   {
  38. >      /* do something */
  39. >   }
  40. >}
  41.  
  42. >Most optimizers will go ahead and lose the comparison since i _has_
  43. >to be equal to 7, right?  Well, not always.. say you have some
  44. >interrupt handler (ahh, the random joy of interrupts) that might
  45. >come in and modify the value of i (highly unlikely, I admit, but this
  46. >is a highly oversimplified example).  Well, if the optimizer
  47. >cuts out the test, then it won't matter whether the handler came
  48. >in.  The correct version of foo() for this app would look like this:
  49.  
  50. >void foo(void)
  51. >{
  52. >   volatile int i = 7;
  53.  
  54. >   if (i == 7)
  55. >   {
  56. >      /* do something */
  57. >   }
  58. >}
  59.  
  60. >Basically, like most modifiers, volatile is a hint to the compiler
  61. >that the value of i could change at any time, and not to take its
  62. >value for granted.  Of course, the compiler could ignore you since
  63. >it's only a hint, but that wouldn't be a very good compiler :)
  64.  
  65.      No, it wouldn't because it would be broken.  K&R2, used as a sub
  66. for the Standard by those of us without $120 or so to spend on said
  67. Standard, says on page 211 (in the indented portion at the bottom):
  68.      "The purpose of volatile is to force an implementation to
  69. suppress optimization that could otherwise occur."
  70.      Note the word "force".
  71.  
  72. >Hope this helps,
  73. >Greg
  74. >--
  75. >Greg Bacon <gbacon@cs.uah.edu>
  76. >University of Alabama in Huntsville
  77. >CS Department Systems Support Team
  78.  
  79. Sincerely,
  80.  
  81. Gene Wirchenko
  82.  
  83. C Pronunciation Guide:
  84.      y=x++;     "wye equals ex plus plus semicolon"
  85.      x=x++;     "ex equals ex doublecross semicolon"
  86.  
  87.